package com.kingtone.rongji.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kingtone.rongji.MainActivity;
import com.kingtone.rongji.R;
import com.kingtone.rongji.biz.CommonMessage;
import com.kingtone.rongji.network.IRequestHandler;
import com.kingtone.rongji.ui.ToastManager;

public class BaseFragment extends Fragment implements IRequestHandler{

	protected MainActivity mActivity;
	
	protected ImageView backBtn;
	
	protected IBaseFragment mBaseInterface;
	
	protected StackFragment stackFragment;
	
	public StackFragment getStackFragment() {
		return stackFragment;
	}

	public void setStackFragment(StackFragment stackFragment) {
		this.stackFragment = stackFragment;
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.mActivity = (MainActivity) this.getActivity();
		if (!(getActivity() instanceof IBaseFragment)) {
			throw new ClassCastException(
					"Hosting Activity must implement BackHandledInterface");
		} else {
			this.mBaseInterface = (IBaseFragment) getActivity();
		}
	}

	@Override
	public void onStart() {
		super.onStart();
		mBaseInterface.setFragment(this);
	}

	@Override
	public void onStop() {
		super.onStop();
		mBaseInterface.setFragment(null);
	}

	public boolean onBackPressed() {
		return stackFragment != null && stackFragment.onBackPressed();
	}
	
	@Override
	public void onNetworkFailure() {
		// TODO Auto-generated method stub
		ToastManager.show(mActivity, mActivity.getResources().getString(R.string.networkError));
	}

	@Override
	public void onBussinessSuccess() {
		// TODO Auto-generated method stub
	}

	@Override
	public void onBussinessError(String errorMessage) {
		// TODO Auto-generated method stub
		ToastManager.show(mActivity, errorMessage);
	}

	@Override
	public void onBussinessSuccess(Object obj) {
		// TODO Auto-generated method stub
	}

	@Override
	public Context getContext() {
		// TODO Auto-generated method stub
		return mActivity;
	}
	
	protected void initTitleView(View view, String titleStr) {
		TextView title = (TextView) view.findViewById(R.id.title);
		title.setText(titleStr);
		backBtn = (ImageView)view.findViewById(R.id.back);
		backBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) 
			{
				getActivity().onBackPressed();
			}
		});
	}

	@Override
	public void onBussinessSuccessWithToast(CommonMessage common) {
		// TODO Auto-generated method stub
	}

}
